package com.login.learn.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/wx")
public class LoginController {

    @Autowired
    RedisTemplate redisTemplate;

    @RequestMapping("/login")
    public String login() throws InterruptedException {
//        {"id":54834,"userId":110412111,"appId":3596,"tableId":109304,"targetId":6,"notifyInfo":"考拉在\"8uiu\"中创建了一条标题为\"无标题\"，请尽快处理","readFlag":0,"createByUserId":110412111,"type":1,"notifyType":2,"notifySubType":5,"notifyInfoJson":"{\"userNick\":\"考拉\",\"tableName\":\"8uiu\",\"taskName\":\"无标题\",\"notifyInfo\":\"考拉在\\\"8uiu\\\"中创建了一条标题为\\\"无标题\\\"，请尽快处理\"}","alarmTime":"2021-03-15 19:03:51"}

        String value1 = "{\"id\":10000,\"userId\":11041211,\"appId\":3596,\"tableId\":109304,\"targetId\":6,\"notifyInfo\":\"考拉在\\\"8uiu\\\"中创建了一条标题为\\\"无标题\\\"，请尽快处理\",\"readFlag\":0,\"createByUserId\":110412111,\"type\":1,\"notifyType\":2,\"notifySubType\":5,\"notifyInfoJson\":\"{\\\"userNick\\\":\\\"考拉\\\",\\\"tableName\\\":\\\"8uiu\\\",\\\"taskName\\\":\\\"无标题\\\",\\\"notifyInfo\\\":\\\"考拉在\\\\\\\"8uiu\\\\\\\"中创建了一条标题为\\\\\\\"无标题\\\\\\\"，请尽快处理\\\"}\",\"alarmTime\":\"2021-03-15 19:03:51\"}";
        for (int i = 0; i < 30; i++) {
            String value = "{\"id\":"+i+",\"userId\":110412111,\"appId\":3596,\"tableId\":109304,\"targetId\":6,\"notifyInfo\":\"考拉在\\\"8uiu\\\"中创建了一条标题为\\\"无标题\\\"，请尽快处理\",\"readFlag\":0,\"createByUserId\":110412111,\"type\":1,\"notifyType\":2,\"notifySubType\":5,\"notifyInfoJson\":\"{\\\"userNick\\\":\\\"考拉\\\",\\\"tableName\\\":\\\"8uiu\\\",\\\"taskName\\\":\\\"无标题\\\",\\\"notifyInfo\\\":\\\"考拉在\\\\\\\"8uiu\\\\\\\"中创建了一条标题为\\\\\\\"无标题\\\\\\\"，请尽快处理\\\"}\",\"alarmTime\":\"2021-03-15 19:03:51\"}";
            redisTemplate.convertAndSend("websocket.notify.channel", value);
        }
        redisTemplate.convertAndSend("websocket.notify.channel", value1);
        return "login";
    }

    @RequestMapping("/test")
    public String test(HttpServletRequest request, HttpServletResponse response) throws InterruptedException {
        Enumeration<String> headerNames = request.getHeaderNames();
        // hasMoreElements = 检测Enumeration中是否还有元素，有则返回true 继续循环，无则返回false 结束循环
        while (headerNames.hasMoreElements()){
            // nextElement = 获取Enumeration中的下一个元素
            String headerName = headerNames.nextElement();
            // getHeader = 根据请求头名字获取请求头内容
            String header = request.getHeader(headerName);
            System.out.println(headerName+"="+ header);
        }
        return "login2";
    }

    public static void main(String[] args) {
        System.out.println(judgeContainsStr("st1231"));

    }
    public static boolean judgeContainsStr(String str) {
        String regex=".*[a-zA-Z]+.*";
        Matcher m= Pattern.compile(regex).matcher(str);
        return m.matches();
    }

}
